/*
 * Project: Daycare Web Application
 * Package: com.madetoordersoftware.daycare.control
 * File:    ChildCtrl
 * Created: Jun 3, 2010
 */
/**
 * Copyright (c) 2010 Made 2 Order Software, LLC
 *
 * All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 * transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 * You may not distribute the URL or redistribute the files to third parties. You may transfer the files onto
 * a single storage device so long as you can prove ownership of the software. You may not reverse engineer,
 * decompile, or disassemble the software or media files. You may not modify or translate the software or
 * media, or distribute copies of the software or media without the written consent of Made 2 Order Software,
 * LLC.
 */
package com.made2ordersoftware.daycare.control;

import com.made2ordersoftware.control.AbstractCtrl;
import com.made2ordersoftware.daycare.business.ChildMgr;
import com.made2ordersoftware.daycare.control.util.JsfUtil;
import com.made2ordersoftware.daycare.domain.enums.Gender;
import com.made2ordersoftware.exceptions.DAOException;
import com.made2ordersoftware.service.exceptions.NonexistentEntityException;
import com.made2ordersoftware.util.ExceptionHandler;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityExistsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Control class for all child actions.
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
@Named("child")
@RequestScoped
public class ChildCtrl extends AbstractCtrl {

    private static final Logger LOG = LoggerFactory.getLogger(ChildCtrl.class);
    @Inject
    private ChildMgr manager;
    private Long parentId;
    private String firstName;
    private String lastName;
    private Date birthDate;
    private Gender gender;
    private String nickname;
    private String comments;
    private Gender genders;
    private Date minDate;
    private Date maxDate;

    /**
     * Creates a new instance of <code>ChildCtrl</code>.
     */
    public ChildCtrl() {
    }

    /**
     * Adds a new child to the system.
     *
     * @return jsf page to navigate to when complete.
     */
    public String create() {
        LOG.info("Creating child in control class.");
        String nextPage = null;
        Exception exp = null;
        String message = null;
        try {
            manager.addChild(getParentId(), getFirstName(), getLastName(), getNickname(), getBirthDate(),
                    getComments(), getGender());
            message = ResourceBundle.getBundle("/Bundle").getString("child_created");
            nextPage = "/home";
            LOG.info("Child created successfully.");
        } catch (EntityExistsException e) {
            exp = e;
            message = ResourceBundle.getBundle("/Bundle").getString("person_already_exist");
        } catch (DAOException e) {
            exp = e;
            message = ResourceBundle.getBundle("/Bundle").getString("server_error_occurred");
            nextPage = "serverError";
        } catch (NonexistentEntityException e) {
            exp = e;
            message = ResourceBundle.getBundle("/Bundle").getString("person_id_does_not_exist");
        } finally {
            if (exp != null) {
                ExceptionHandler.handleException(exp);
                JsfUtil.addErrorMessage(message);
            } else {
                JsfUtil.addSuccessMessage(message);
            }
        }
        return nextPage;
    }

    /**
     * Reads the child information from the system.
     *
     * @return jsf page to navigate to when complete.
     */
    public String read() {
        return "/home";
    }

    /**
     * Updates the child information in the system.
     *
     * @return jsf page to navigate to when complete.
     */
    public String update() {
        Exception e = null;
        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ChildUpdated"));
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        return "/home";
    }

    /**
     * Sets the child status to inactive.
     *
     * @return jsf page to navigate to when complete.
     */
    public String delete() {
        Exception e = null;
        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ChildDeleted"));
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        return "/home";
    }

    /**
     * @return the firstName of child
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName of child
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the birthDate of child
     */
    public Date getBirthDate() {
        return birthDate;
    }

    /**
     * @param birthDate the birthDate to set
     */
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    /**
     * @return the gender of child
     */
    public Gender getGender() {
        return gender;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(Gender gender) {
        this.gender = gender;
    }

    /**
     * @return the nickname of child
     */
    public String getNickname() {
        return nickname;
    }

    /**
     * @param nickname the nickname to set
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    /**
     * @return any comments about the child
     */
    public String getComments() {
        return comments;
    }

    /**
     * @param comments the comments to set
     */
    public void setComments(String comments) {
        this.comments = comments;
    }

    /**
     * @return the parentId associated with this child
     */
    public Long getParentId() {
        return parentId;
    }

    /**
     * @param parentId the parentId to set
     */
    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    /**
     * @return list of genders for JSF dropdown
     */
    public Gender[] getGenders() {
        return Gender.values();
    }

    /**
     *
     * @return minDate - Earliest date of birth allowed for day care..
     */
    public Date getMinDate() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -12);
        return new Date(c.getTimeInMillis());
    }

    /**
     *
     * @return maxDate - Today's date.
     */
    public Date getMaxDate() {
        return new Date();
    }
}
